﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SimpleGame.Font;

namespace SimpleGame.ScreenManagement
{
    public class MenuItem
    {
        #region Fields

        // The text to be drawn for this menu item
        private string text;
        // The position where this menu item should be drawn
        private Vector2 position;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the text of this menu item.
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }


        /// <summary>
        /// Gets or sets the position at which this menu item should be drawn.
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        #endregion

        #region Constructor

        public MenuItem(string text)
        {
            this.text = text;
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Event raised when the menu item is selected.
        /// </summary>
        public event EventHandler Selected;


        /// <summary>
        /// Method for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelectItem(EventArgs e)
        {
            if (Selected != null)
                Selected(this, e);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Updates the menu item.
        /// </summary>
        public virtual void Update(GameTime gameTime, bool isSelected) {}

        public virtual void Draw(GameTime gameTime, bool isSelected)
        {
            // Set color wether the menu item is selected or not
            Color color = Color.White;
            float scale = 1.0f;
            if (isSelected)
            {
                color = Color.OrangeRed;

                // Pulsate the size of the selected menu item
                double time = gameTime.TotalGameTime.TotalSeconds;
                float pulsate = (float)Math.Sin(time * 6) + 1;
                scale = 1 + pulsate * 0.05f;
            }

            // Draw the menu item
            FontManager.Instance.DrawString(
                "menuitem",
                text,
                position,
                color,
                scale,
                0f,
                Vector2.Zero);
        }

        /// <summary>
        /// Gets how much space in terms of height the menu item needs
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return FontManager.Instance.GetFont("menuitem").SpriteFont.LineSpacing;
        }


        /// <summary>
        /// Gets how much space in terms of width the menu item needs
        /// </summary>
        public virtual int GetWidth(MenuScreen screen)
        {
            return (int)FontManager.Instance.GetFont("menuitem").SpriteFont.MeasureString(Text).X;
        }
        #endregion
    }
}
